package com.github.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.base.annotation.SysLog;
import com.github.base.dto.user.UserAddDTO;
import com.github.base.dto.user.UserListDTO;
import com.github.base.dto.user.UserUpdateDTO;
import com.github.base.service.UserService;
import com.github.base.utils.Result;
import com.github.base.vo.user.UserInfoVO;
import com.github.base.vo.user.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 账号
 * @author: dongdong.wen
 * @date: 2021/1/28 3:40 下午
 **/
@RestController
@Tag(name = "账号模块")
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;


    @PostMapping("/save")
    @Operation(summary = "用户信息添加API")
    @ApiOperationSupport(order = 1)
    @SysLog(module = "账号模块", value = "新增账号信息")
    public Result<?> save(@Validated @RequestBody UserAddDTO dto) {
        userService.save(dto);
        return Result.ok();
    }

    @Operation(summary = "账号-分页查询")
    @PostMapping(value = "/pageList")
    public Result<Page<UserListVO>> pageList(@Validated @RequestBody UserListDTO userListDTO) {
        Page<UserListVO> pageList = userService.pageList(userListDTO);
        return Result.ok(pageList);
    }

    @PostMapping(value = "/update")
    @Operation(summary = "修改", description = "修改账号信息Api")
    @SysLog(module = "账号模块", value = "修改账号信息")
    public Result<?> update(@Validated @RequestBody UserUpdateDTO userUpdateDTO) {
        userService.update(userUpdateDTO);
        return Result.ok();
    }

    @PostMapping(value = "/resetPassword/{id}")
    @Operation(summary = "重置密码")
    @SysLog(module = "账号模块", value = "重置密码")
    public Result<?> resetPassword(@Parameter(description = "用户id") @PathVariable(value = "id") Long id) {
        userService.resetPassword(id);
        return Result.ok();
    }

    @PostMapping(value = "/delete/{id}")
    @Operation(summary = "删除账号信息")
    @SysLog(module = "账号模块", value = "删除账号")
    public Result<?> delete(@Parameter(description = "用户id") @PathVariable(value = "id") Long id) {
        userService.delete(id);
        return Result.ok();
    }

    @PostMapping(value = "/switchState/{id}")
    @Operation(summary = "切换启用状态")
    @SysLog(module = "账号模块", value = "切换启用状态")
    public Result<?> switchState(@Parameter(description = "用户id") @PathVariable(value = "id") Long id) {
        userService.switchState(id);
        return Result.ok();
    }

    @GetMapping("/currentInfo")
    @Operation(summary = "当前用户信息")
    public Result<UserInfoVO> currentInfo() {
        UserInfoVO userInfoVO = userService.currentInfo();
        return Result.ok(userInfoVO);
    }


}
